#! /usr/bin/python3
# -*- coding: UTF-8 -*-
"""
  作者: 小肥爬爬
  简书: https://www.jianshu.com/u/db796a501972
  gitee: https://gitee.com/xiaofeipapa/python-toolkit
  您可以自由转载此博客文章, 恳请保留原链接, 谢谢!
"""
from fabric import Connection
import fab_utils


def do_it():

    host = '192.168.0.12'
    user = 'root'
    password = 'test'

    # ssh 连接的正确姿势
    conn = Connection(host=host, user=user, connect_kwargs={'password': password})

    # 结果变得更简单了
    cmd = 'ls /tmp'
    result = fab_utils.run(conn, cmd)

    # 正常运行时, 信息在 stdout里
    print('-------- 下面是结果')
    print(result)

    # 出错时, 程序会抛出异常
    # 来一个出错的例子, tmp/xiaofeipapa 目录不存在
    cmd = 'ls /tmp/xiaofeipapa'
    result = fab_utils.run(conn, cmd)
    print(result)


if __name__ == '__main__':
    do_it()

